package cn.iocoder.yudao.module.erp.service.purchaseplan.purchaseengineeringdetail;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseengineeringdetail.PurchaseEngineeringDetailPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseengineeringdetail.PurchaseEngineeringDetailSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchaseplan.purchaseengineeringdetail.PurchaseEngineeringDetailDO;
import cn.iocoder.yudao.module.erp.dal.mysql.purchaseplan.purchaseengineeringdetail.PurchaseEngineeringDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.PURCHASE_ENGINEERING_DETAIL_NOT_EXISTS;

/**
 * 工程采购计划明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PurchaseEngineeringDetailServiceImpl implements PurchaseEngineeringDetailService {

    @Resource
    private PurchaseEngineeringDetailMapper purchaseEngineeringDetailMapper;

    @Override
    public void createPurchaseEngineeringDetailBatch(List<PurchaseEngineeringDetailSaveReqVO> createReqVOS) {
        // 插入
        List<PurchaseEngineeringDetailDO> purchaseEngineeringDetailS = BeanUtils.toBean(createReqVOS, PurchaseEngineeringDetailDO.class);
        purchaseEngineeringDetailMapper.insertBatch(purchaseEngineeringDetailS);
    }

    @Override
    public void updatePurchaseEngineeringDetail(PurchaseEngineeringDetailSaveReqVO updateReqVO) {
        // 校验存在
        validatePurchaseEngineeringDetailExists(updateReqVO.getId());
        // 更新
        PurchaseEngineeringDetailDO updateObj = BeanUtils.toBean(updateReqVO, PurchaseEngineeringDetailDO.class);
        purchaseEngineeringDetailMapper.updateById(updateObj);
    }

    @Override
    public void deletePurchaseEngineeringDetail(Long id) {
        // 校验存在
        validatePurchaseEngineeringDetailExists(id);
        // 删除
        purchaseEngineeringDetailMapper.deleteById(id);
    }

    @Override
        public void deletePurchaseEngineeringDetailListByIds(List<Long> ids) {
        // 校验存在
        validatePurchaseEngineeringDetailExists(ids);
        // 删除
        purchaseEngineeringDetailMapper.deleteByIds(ids);
        }

    private void validatePurchaseEngineeringDetailExists(List<Long> ids) {
        List<PurchaseEngineeringDetailDO> list = purchaseEngineeringDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PURCHASE_ENGINEERING_DETAIL_NOT_EXISTS);
        }
    }

    private void validatePurchaseEngineeringDetailExists(Long id) {
        if (purchaseEngineeringDetailMapper.selectById(id) == null) {
            throw exception(PURCHASE_ENGINEERING_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public PurchaseEngineeringDetailDO getPurchaseEngineeringDetail(Long id) {
        return purchaseEngineeringDetailMapper.selectById(id);
    }

    @Override
    public PageResult<PurchaseEngineeringDetailDO> getPurchaseEngineeringDetailPage(PurchaseEngineeringDetailPageReqVO pageReqVO) {
        return purchaseEngineeringDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteByPlanId(Long planId) {
        purchaseEngineeringDetailMapper.deleteByPlanId(planId);
    }

    @Override
    public List<PurchaseEngineeringDetailDO> getByPlanId(Long planId) {
        return purchaseEngineeringDetailMapper.selectByPlanId(planId);
    }

}